package czh.cocoa.ui;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.util.DisplayMetrics;
import czh.cocoa.foundation.NSData;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGContext;
import czh.cocoa.graphics.CGImage;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;

import static java.lang.Math.*;
import static czh.cocoa.ui.UIKit.*;
import static czh.cocoa.graphics.CGContext.*;

public class UIImage extends NSObject {
	
	private CGImage mImage;
	
	private UIEdgeInsets mCapInsets;
	
	public static UIImage imageNamed(NSString name) {
		Context ctx = UIApplication.sharedApplication().getContext();
		AssetManager am = ctx.getAssets();
		try {
			String name0 = name.toString();
			if (name0.lastIndexOf(".") == -1) {
				 name0 += ".png";
			}
			InputStream in = am.open(name0);
			Bitmap bitmap = BitmapFactory.decodeStream(in);
			CGImage image = new CGImage(bitmap);
			in.close();
			return new UIImage(image);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static UIImage imageWithContentsOfFile(NSString filepath) {
		try {
			FileInputStream in = new FileInputStream(filepath.toString());
			Bitmap bitmap = BitmapFactory.decodeStream(in);
			CGImage image = new CGImage(bitmap);
			in.close();
			return new UIImage(image);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//addtional
	public static UIImage imageWithBlendColor(UIImage image, UIColor blendColor) {
		CGSize size = image.size();
		UIGraphicsBeginImageContext(size);
		CGContext ctx = UIGraphicsGetCurrentContext();
		Paint paint = ctx.getPaint();
		ColorFilter filter = new PorterDuffColorFilter(blendColor.CGColor().intValue(), 
								 PorterDuff.Mode.SRC_IN);
		paint.setColorFilter(filter);
		image.drawInRect(CGRectMake(0, 0, size.width, size.height));
		paint.setColorFilter(null);
		UIImage image2 = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
		
		return image2;
	}
	
	public UIImage(CGImage image) {
		mImage = image;
		mCapInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
	}
	
	public UIImage(CGImage image, float scale) {
		mImage = new CGImage(image, scale); 
		mCapInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
	}
	
	public UIImage(NSData data) {
		Bitmap bitmap = BitmapFactory.decodeByteArray(data.bytes(), 0, data.length());
		mImage = new CGImage(bitmap);
		mCapInsets = UIEdgeInsetsMake(0, 0, 0, 0); 
	}
	
	public CGImage CGImage() {
		return mImage;
	}
	
	public CGSize size() {
		return mImage.size(); 
	}
	
	public void drawAtPoint(CGPoint point) {
		CGSize size = size();
		CGRect rect = new CGRect(point.x, point.y, size.width, size.height);
		drawInRect(rect);
	}
	
	public void drawInRect(CGRect rect) {
		CGContext ctx = UIGraphicsGetCurrentContext();
		UIEdgeInsets insets = mCapInsets;
		if (insets.top > 0 || insets.left > 0 || insets.right > 0 || insets.bottom > 0) { 
			CGSize image_size = mImage.size();
			if (rect.size.width > image_size.width || rect.size.height > image_size.height) {
				
				float left_inset = insets.left;
				float top_inset = insets.top;
				float right_inset = insets.right;
				float bottom_inset = insets.bottom;
				
				CGContextTranslateCTM(ctx, rect.origin.x, rect.origin.y);
				
				//left-top
				CGRect rect_lt = CGRectMake(0, 0, left_inset, top_inset); 
				if (round(rect_lt.size.width) > 0 && rect_lt.size.height > 0) { 
					CGRect rect_lt2 = CGRectMake(0, 0, left_inset, top_inset); 
					CGImage img_lt = new CGImage(mImage, rect_lt);
					CGContextDrawImage(ctx, rect_lt2, img_lt);
				}
				
				//middle-top
				CGRect rect_mt = CGRectMake(left_inset, 0, 
						image_size.width - left_inset - right_inset, top_inset); 
				if (rect_mt.size.width > 0 && rect_mt.size.height > 0) { 
					CGRect rect_mt2 = CGRectMake(left_inset, 0, 
							rect.size.width - left_inset - right_inset, top_inset); 
					CGImage img_mt = new CGImage(mImage, rect_mt);
					CGContextDrawImage(ctx, rect_mt2, img_mt);
				}
				
				//right-top
				CGRect rect_rt = CGRectMake(image_size.width - right_inset, 0, right_inset, top_inset); 
				if (rect_rt.size.width > 0 && rect_rt.size.height > 0) {
					CGRect rect_rt2 = CGRectMake(rect.size.width - right_inset, 0, right_inset, top_inset); 
					CGImage img_rt = new CGImage(mImage, rect_rt);
					CGContextDrawImage(ctx, rect_rt2, img_rt);
				}
				
				//right-middle
				CGRect rect_rm = CGRectMake(image_size.width - right_inset, top_inset, 
						right_inset, image_size.height - top_inset - bottom_inset); 
				if (rect_rm.size.width > 0 && rect_rm.size.height > 0) {
					CGRect rect_rm2 = CGRectMake(rect.size.width - right_inset, top_inset, 
						right_inset, rect.size.height - top_inset - bottom_inset); 
					CGImage img_rm = new CGImage(mImage, rect_rm);
					CGContextDrawImage(ctx, rect_rm2, img_rm);
				}
				
				//right-bottom
				CGRect rect_rb = CGRectMake(image_size.width - right_inset, image_size.height - bottom_inset, 
						right_inset, bottom_inset); 
				if (rect_rb.size.width > 0 && rect_rb.size.height > 0) {
					CGRect rect_rb2 = CGRectMake(rect.size.width - right_inset, rect.size.height - bottom_inset, 
						right_inset, bottom_inset); 
					CGImage img_rb = new CGImage(mImage, rect_rb);
					CGContextDrawImage(ctx, rect_rb2, img_rb);
				}
				
				//middle-bottom
				CGRect rect_mb = CGRectMake(left_inset, image_size.height - bottom_inset, 
						image_size.width - left_inset - right_inset, bottom_inset); 
				if (rect_mb.size.width > 0 && rect_mb.size.height > 0) {
					CGRect rect_mb2 = CGRectMake(left_inset, rect.size.height - bottom_inset, 
						rect.size.width - left_inset - right_inset, bottom_inset); 
					CGImage img_mb = new CGImage(mImage, rect_mb);
					CGContextDrawImage(ctx, rect_mb2, img_mb);
				}
				
				//left-bottom
				CGRect rect_lb = CGRectMake(0, image_size.height - bottom_inset, left_inset, bottom_inset); 
				if (rect_lb.size.width > 0 && rect_lb.size.height > 0) {
					CGRect rect_lb2 = CGRectMake(0, rect.size.height - bottom_inset, left_inset, bottom_inset); 
					CGImage img_lb = new CGImage(mImage, rect_lb);   
					CGContextDrawImage(ctx, rect_lb2, img_lb);
				}
				
				//left-middle
				CGRect rect_lm = CGRectMake(0, top_inset, left_inset, image_size.height - top_inset - bottom_inset); 
				if (rect_lm.size.width > 0 && rect_lm.size.height > 0) {
					CGRect rect_lm2 = CGRectMake(0, top_inset, left_inset, rect.size.height - top_inset - bottom_inset); 
					CGImage img_lm = new CGImage(mImage, rect_lm);   
					CGContextDrawImage(ctx, rect_lm2, img_lm);
				}
				
				//center
				CGRect rect_center = CGRectMake(left_inset, top_inset, 
						image_size.width - left_inset - right_inset, image_size.height - top_inset - bottom_inset); 
				if (rect_center.size.width > 0 && rect_center.size.height > 0) {
					CGRect rect_center2 = CGRectMake(left_inset, top_inset, 
						rect.size.width - left_inset - right_inset, rect.size.height - top_inset - bottom_inset); 
					CGImage img_center = new CGImage(mImage, rect_center);   
					CGContextDrawImage(ctx, rect_center2, img_center);
				}
				
				CGContextTranslateCTM(ctx, -rect.origin.x, -rect.origin.y);
				
				return;
			}
		}
		
		CGContextDrawImage(ctx, rect, mImage);
	}
	
	public UIImage resizableImageWithCapInsets(UIEdgeInsets capInsets) {
		UIImage image = new UIImage(mImage);
		image.mCapInsets = new UIEdgeInsets(capInsets);
		return image;
	}

}
